﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.modeconfig
{
    public partial class ruleconfig_edit : Web.UI.ManagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("mall_rule_config", DTEnums.ActionEnum.View.ToString()); //检查权限
                //当前环境信息
                Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                string busnieseNum = mgr.busnieseNum;
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
                ShowInfo(busnieseNum, appNum);
            }
        }

        private void ShowInfo(string busnieseNum, string appNum)
        {
            BLL.bs_rule_config bll = new BLL.bs_rule_config();
            var isExist = bll.ExistByNum(busnieseNum, appNum);
            Model.bs_rule_config model = new Model.bs_rule_config();
            if (isExist)
            {
                model = bll.GetModel(busnieseNum, appNum);
            }
            if (model.is_distribution == 1)
            {
                this.cbIsDistribution.Checked = true;
            }
            txtRecommendFollowExp.Text = model.recommendFollowExp.ToString();
            txtFollowExp.Text = model.followExp.ToString();
            if (model.is_follow_Buy == 1)
            {
                this.cbIsFollowBuy.Checked = true;
            }
            txtTwoPieceDiscount.Text = model.twoPieceDiscount.ToString();
            txtUpTwoPieceDiscount.Text = model.upTwoPieceDiscount.ToString();
            txtFreePostMoney.Text = model.freePostMoney.ToString();
            txtMinWithdrawMoney.Text = model.minWithdrawMoney.ToString();
            txtAutoConfirmDay.Text = model.autoConfirmDay.ToString();
            txtLevel0BuyExp.Text = model.level0BuyExp.ToString();
            txtLevel1BuyExp.Text = model.level1BuyExp.ToString();
            txtLevel2BuyExp.Text = model.level2BuyExp.ToString();
            txtLevel3BuyExp.Text = model.level3BuyExp.ToString();
            txtLevel0ReturnScale.Text = model.level0ReturnScale.ToString();
            txtLevel1ReturnScale.Text = model.level1ReturnScale.ToString();
            txtLevel2ReturnScale.Text = model.level2ReturnScale.ToString();
            txtLevel3ReturnScale.Text = model.level3ReturnScale.ToString();
            txtShoppingCurrency.Text = model.shoppingCurrency.ToString();
            txtActionCurrency.Text = model.actionCurrency.ToString();
            txtPublicFund.Text = model.publicFund.ToString();
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.bs_rule_config bll = new BLL.bs_rule_config();
            Model.bs_rule_config model = new Model.bs_rule_config();

            //当前环境信息
            Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            string busnieseNum = mgr.busnieseNum;
            string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
            model.busnieseNum = busnieseNum;
            model.appNum = appNum;
            if (this.cbIsDistribution.Checked == true)
            {
                model.is_distribution = 1;
            }
            model.recommendFollowExp = int.Parse(this.txtRecommendFollowExp.Text.Trim());
            model.followExp = int.Parse(this.txtFollowExp.Text.Trim());
            if (this.cbIsFollowBuy.Checked == true)
            {
                model.is_follow_Buy = 1;
            }
            model.twoPieceDiscount = decimal.Parse(this.txtTwoPieceDiscount.Text.Trim());
            model.upTwoPieceDiscount = decimal.Parse(this.txtUpTwoPieceDiscount.Text.Trim());
            model.freePostMoney = decimal.Parse(this.txtFreePostMoney.Text.Trim());
            model.minWithdrawMoney = decimal.Parse(this.txtMinWithdrawMoney.Text.Trim());
            model.autoConfirmDay = int.Parse(this.txtAutoConfirmDay.Text.Trim());
            model.level0BuyExp = decimal.Parse(this.txtLevel0BuyExp.Text.Trim());
            model.level1BuyExp = decimal.Parse(this.txtLevel1BuyExp.Text.Trim());
            model.level2BuyExp = decimal.Parse(this.txtLevel2BuyExp.Text.Trim());
            model.level3BuyExp = decimal.Parse(this.txtLevel3BuyExp.Text.Trim());
            model.level0ReturnScale = decimal.Parse(this.txtLevel0ReturnScale.Text.Trim());
            model.level1ReturnScale = decimal.Parse(this.txtLevel1ReturnScale.Text.Trim());
            model.level2ReturnScale = decimal.Parse(this.txtLevel2ReturnScale.Text.Trim());
            model.level3ReturnScale = decimal.Parse(this.txtLevel3ReturnScale.Text.Trim());
            model.shoppingCurrency = decimal.Parse(this.txtShoppingCurrency.Text.Trim());
            model.actionCurrency = decimal.Parse(this.txtActionCurrency.Text.Trim());
            model.publicFund = decimal.Parse(this.txtPublicFund.Text.Trim());

            if (bll.Edit(model) == true)
            {
                JscriptMsg("商城规则配置保存成功！", string.Empty);
            }
            else
            {
                JscriptMsg("商城规则配置保存失败，请重新尝试！", string.Empty);
            }
        }
    }
}